package com.java.multipledatasource.spring.test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author huxb
 * @date 2020/10/15 0015 上午 9:01
 * @Desciption
 */
public class ImageSynthesisUtils {
    /**
     * 合成图片宽度
     */
    private  static  Integer imageHeight = 596;
    /**
     * 合成图片长度
     */
    private static  Integer imageWeight = 596;
    /**
     *
     * @Title: modifyImagetogeter
     * @Description: 将几张图片合成一张图片
     * @param: @param b1
     * @param: @param b2
     * @param: @param b3
     * @param: @return
     * @return: BufferedImage
     * @throws
     * @author:石德斌
     * @date:2020年4月21日 下午3:26:41
     */
    private static BufferedImage modifyImagetogeter(BufferedImage[] image) {
        //创建图片
        BufferedImage tag = new BufferedImage(imageWeight, imageHeight, BufferedImage.TYPE_INT_RGB);
        try {
            Graphics2D graphics = tag.createGraphics();
            //设置颜色
            graphics.setColor(new Color(255,255,255));
            //填充颜色
            graphics.fillRect(0, 0, imageWeight, imageHeight);
            //                int startX = 0;
//                int startY = 0;
//                BufferedImage b1 = image[0];
//                graphics.drawImage(b1, startX, startY, 596, 402, null);
//                BufferedImage b2 = image[1];
//                startX = 0;
//                startY = 404;
//                graphics.drawImage(b2, startX, startY, 297, 192, null);
//                BufferedImage b3 = image[2];
//                startX = startX + 300;
//                graphics.drawImage(b3, startX, startY, 297, 192, null);

            int startX = 0;
            int startY = 0;
            BufferedImage b1 = image[0];
            graphics.drawImage(b1, startX, startY, 296, 596, null);
            BufferedImage b2 = image[1];
            startX = 298;
            graphics.drawImage(b2, startX, startY, 298, 296, null);
            BufferedImage b3 = image[2];
            startY = 298;
            graphics.drawImage(b3, startX, startY, 298, 298, null);

            graphics.dispose();
        } catch (Exception e) {
         //   logger.error("推送同比压缩图片出错{}",e);
        }

        return tag;
    }

    /**
     * 图片加载
     * @param imgName
     * @return
     */
    private static BufferedImage loadImageLocal(String imgName) {
        try {
            return ImageIO.read(new File(imgName));
//        	return ImageIO.read(new URL(imgName));
        } catch (IOException e) {
//            logger.error("推送同比压缩图片出错{}",e);
        }
        return null;
    }

    /**
     * 写入磁盘
     * @param newImage
     * @param img
     */
    private static void writeImageLocal(String newImage, BufferedImage img) {
        if (newImage != null && img != null) {
            try {
                File outputfile = new File(newImage);
                ImageIO.write(img, "jpg", outputfile);
            } catch (IOException e) {
             //  logger.error("推送同比压缩图片出错{}",e);
            }
        }
    }

    /**
     *
     * @Title: handleLarge
     * @Description: 同比例压缩图片，使图片形成相同大小
     * @param: @param image
     * @param: @return
     * @return: BufferedImage[]
     * @throws
     * @author:石德斌
     * @date:2020年4月23日 上午10:36:11
     */
    private static BufferedImage[] handleLarge(Integer width,Integer height,BufferedImage[] image) {
        BufferedImage[] b = new BufferedImage[image.length];
        for (int i = 0; i < image.length; i++) {
            BufferedImage sourceImage = image[i];
            try {
                if (i == 0) {
                    b[i] = ImageSynthesisUtils.zoom2(width * 2, width, sourceImage);
                } else {
                    b[i] = zoom2(width , width, sourceImage);
                }
            } catch (Exception e) {
               // logger.error("推送同比压缩图片出错{}",e);
            }
        }
        return b;
    }


    public static void imageSynthesis (String newImagePath, List<String> imgPathList) {
        BufferedImage[] image =new BufferedImage[imgPathList.size()];
        for (int i = 0; i < imgPathList.size(); i++) {
            image[i]=loadImageLocal(imgPathList.get(i));
        }
        writeImageLocal(newImagePath , modifyImagetogeter(handleLarge(148 * 2,96 * 2,image)));
    }

    public static void main(String[] args) throws Exception {
        List<String> list = new ArrayList<>(10);
        list.add("F:\\HY\\666\\1.jpg");
        list.add("F:\\HY\\666\\2.jpg");
        list.add("F:\\HY\\666\\3.jpg");
        imageSynthesis("F:\\HY\\666\\4.jpg", list);
//        try {
//            //用于合成的图片数量
//            int count = 3;
//            BufferedImage[] image =new BufferedImage[count];
//            for(int i =0;i<count;i++){
//
//                if (i == 2) {
//                    image[i]=loadImageLocal("F:\\HY\\imgcorn\\1603262740207.jpg");
//                } else {
//                    image[i]=loadImageLocal("F:\\HY\\imgcorn\\1603262742363.jpg");
//                }
//            }
//            writeImageLocal("F:\\HY\\imgcorn"+count+".png", modifyImagetogeter(handleLarge(88,88,image)));
//        } catch (Exception e) {
//            e.printStackTrace();
//            System.out.println(e.getMessage()  );
//        }

    }

    private static BufferedImage zoom2(int width,int height,BufferedImage sourceImage) throws Exception {

        if( sourceImage == null ){
            return sourceImage;
        }
        // 计算x轴y轴缩放比例--如需等比例缩放，在调用之前确保參数width和height是等比例变化的
        double ratiox  = (new Integer(width)).doubleValue()/ sourceImage.getWidth();
        double ratioy  = (new Integer(height)).doubleValue()/ sourceImage.getHeight();
        AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratiox, ratioy), null);
        BufferedImage bufImg = op.filter(sourceImage, null);
        return bufImg;
    }

}
